# -*- coding:utf-8 -*-
'''
@author: xiaoming
@contact: lishihui0129@163.com
@time: 2017/6/29 11:53
@desc:
'''
import re
import os
from myUtil import qqPattern,flagSplitPattern
from mysqlHelper import connectDB,close,insertSql


def qqExtract(string):
    '''
    :param string: 待提取对象
    :return:提取结果
    '''
    res=re.findall(qqPattern,string)
    # 去除重复
    res=list(set(res))
    # 提取前缀，并进行组合
    # flag=prefixFlag(string)
    temp=[]
    for item in res:
        linshi=item
        temp.append(linshi)
    return temp
def prefixFlag(string):
    '''    mmmmmm,前缀QQ
    :param string: 待提取对象
    :return:前缀信息
    '''
    if 'Q' in string:
        ind=string.index('Q')
    elif 'q' in string:
        ind=string.index('q')
    elif '扣' in string:
        ind=string.index('扣')
    else:
        ind=0

    temp=string[:ind]
    res=re.split(r'，|:|!|,|;| ',temp)
    return res[-1].strip()
#     .replace("：","").replace(":","")
def insertQqTable(string):
    '''
    对识别的结果存入数据库
    :param string:
    :return:
    '''
    res=re.findall(qqPattern,string)
    res=list(set(res))
    flag=prefixFlag(string)
    connectDB()
    sql="insert into qq(qq_value,prefix) values(%s,%s)"

    for each in res:
        param=(each,flag)
        insertSql(sql,param)
    close()


if __name__=='__main__':
    workdir='.\output'
    fileList=os.listdir(workdir)
    if 'qq.txt' in fileList:
        targetFile=os.path.join(workdir,'qq.txt')
        res=open(targetFile).readlines()
        for line in res:
            print qqExtract(line)
            insertQqTable(line)
    else:
        pass